<template>
  <zeda-dialog
    v-model="visible"
    title="新增导航菜单"
    width="500px"
    :before-close="handleClose"
    style="margin-top: 220px"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <form-button
      slot="footer"
      :loading="loading"
      @cancel="handleClose"
      @submit="submit"
    >
    </form-button>
  </zeda-dialog>
</template>

<script>
import { listMenu } from "@/api/system/menu";
import { addItem } from "@/api/navset/index.js";

import CascaderSelect from "@/components/Select/CascaderSelect";
import FormButton from "@/components/FormButton";
import { getDynamicMenutree, addBindRole } from "@/api/navset/index.js";

export default {
  dicts: ["energy_type"],
  components: {
    FormButton,
    CascaderSelect,
  },
  props: {
    // 此处的ID即为操作新增的导航模块的id即为 parentId
    id: {
      type: [Number, String],
      default: "",
    },
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false, // 按钮加载中判断
      menuList: [],
      choseMenuname: "",
      apsForm: {
        formRef: "form",
        formData: {},
        formConfig: {
          formAttr: {
            // inline: true,
            "label-position": "right",
            size: "small",
            bsColumn: {
              col: 1,
              middle: true,
            },
            rules: {
              sysMenuId: [
                {
                  required: true,
                  message: "请选择系统菜单",
                  trigger: "change",
                },
              ],
            },
          },
          properties: {
            sysMenuId: {
              label: "系统菜单",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [...that.menuList];
                let valNum = that.$refs?.form?.getFieldsValue().sysMenuId;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择系统菜单",
                        value: valNum,
                        clearable: true,
                        filterable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue("sysMenuId", value);
                          that.menuList.map((item) => {
                            if (item.value == value) {
                              that.choseMenuname = item.label;
                            }
                          });
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: value,
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
            orderNum: {
              label: "排序",
              typeName: "el-input-number",
            },
          },
        },
      },
    };
  },
  watch: {
    visible: {
      handler(value) {
        if (value) {
          this.getMenulist();
          if (String(this.id).length > 0) {
          }
        }
      },
      deep: false,
      immediate: false,
    },
  },
  methods: {
    getMenulist() {
      this.menuList = [];
      listMenu({})
        .then((res) => {
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          let arr = res?.data ?? [];
          if (arr.length > 0) {
            arr.map((item) => {
              if (item.menuType == "C") {
                if (item.status == "0") {
                  let obj = {
                    label: item?.menuName,
                    value: item?.menuId,
                  };
                  // console.log(item)
                  this.menuList.push(obj);
                }
              }
            });
          }
        })
        .catch(() => {
          // this.$message.error("系统错误,请联系管理员!");
        });
      // console.log(this.menuList, "menuList");
    },
    submit() {
      const data = {
        ...this.$refs.form.getFieldsValue(),
      };
      data.configId = 1; // 赋予配置对象的id(这里是登录导航的ID为1)
      data.parentId = this.id;
      data.name = this.choseMenuname;
      data.subheading = this.choseMenuname;
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.addRequest(data);
        } else {
          return false;
        }
      });
    },
    addRequest(data) {
      addItem(data)
        .then((res) => {
          this.loading = false;
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          this.$message.success("操作成功");
          this.handleClose(true);
        })
        .catch(() => {
          this.loading = false;
          // this.$message.error("系统错误,请联系管理员!");
        });
    },
    handleClose(status) {
      this.$refs["form"].resetFields();
      this.$emit("update:visible", false);
      this.$emit("close", status === true ? status : false);
    },
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-form {
  .el-input,
  .el-cascader,
  .el-input-number,
  .el-select {
    width: 100% !important;
  }
  .map {
    display: flex;
    .el-input {
      margin-right: 10px;
    }
  }
}
</style>
